/**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
 */

package com.sys.midware.zookeeper.sample.zkclient;

import java.util.Arrays;
import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;

import com.sys.midware.logger.Logger;
import com.sys.midware.logger.LoggerFactory;

/**
 * <b>ClassName:</b> ZooKeeperOperator.java <br/>
 * <b>Description:</b> TODO <br/>
 * <b>Date:</b>     2016年5月4日 上午11:12:02<br/> 
 * @author   mobing
 * @version  	 
 */
public class ZooKeeperOperator extends AbstractZK {

    private static Logger log = LoggerFactory.getLogger(ZooKeeperOperator.class.getName());  
    /** 
     *  
     *<b>function:</b>创建持久态的znode,比支持多层创建.比如在创建/parent/child的情况下,无/parent.无法通过 
     *@author cuiran 
     *@createDate 2013-01-16 15:08:38 
     *@param path 
     *@param data 
     *@throws KeeperException 
     *@throws InterruptedException 
     */  
    public void create(String path,byte[] data)throws KeeperException, InterruptedException{  
        /** 
         * 此处采用的是CreateMode是PERSISTENT  表示The znode will not be automatically deleted upon client's disconnect. 
         * EPHEMERAL 表示The znode will be deleted upon the client's disconnect. 
         */   
        this.zooKeeper.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);  
    }  
    /** 
     *  
     *<b>function:</b>获取节点信息 
     *@author cuiran 
     *@createDate 2013-01-16 15:17:22 
     *@param path 
     *@throws KeeperException 
     *@throws InterruptedException 
     */  
    public void getChild(String path) throws KeeperException, InterruptedException{     
        try{  
            List<String> list=this.zooKeeper.getChildren(path, false);  
            if(list.isEmpty()){  
                log.debug(path+"中没有节点");  
            }else{  
                log.debug(path+"中存在节点");  
                for(String child:list){  
                    log.debug("节点为："+child);  
                }  
            }  
        }catch (KeeperException.NoNodeException e) {  
            // TODO: handle exception  
             throw e;     
  
        }  
    }  
      
    public byte[] getData(String path) throws KeeperException, InterruptedException {     
        return  this.zooKeeper.getData(path, false,null);     
    }    
    
    public static void main(String[] args) {  
        try {     
               ZooKeeperOperator zkoperator             = new ZooKeeperOperator();     
               zkoperator.connect("172.16.1.7:2181,172.16.1.8:2181");  
                 
               byte[] data = new byte[]{'a','b','c','d'};     
                    
//             zkoperator.create("/momo/node1",data);     
             System.out.println("----" + Arrays.toString(zkoperator.getData("/momo")));     
//                  
//             zkoperator.create("/root/child1",data);     
//             System.out.println(Arrays.toString(zkoperator.getData("/root/child1")));     
//                  
//             zkoperator.create("/root/child2",data);     
//             System.out.println(Arrays.toString(zkoperator.getData("/root/child2")));     
                    
//               String zktest="ZooKeeper的Java API测试";  
//               zkoperator.create("/root/child3", zktest.getBytes());  
//               log.debug("获取设置的信息："+new String(zkoperator.getData("/root/child3")));  
//                 
//               System.out.println("节点孩子信息:");     
//               zkoperator.getChild("/root");     
                    
               zkoperator.close();     
                 
                 
           } catch (Exception e) {     
               e.printStackTrace();     
           }     
 
   }  
}

